package org.cloud.dimension.config;

import java.util.ArrayList;
import java.util.List;
import lombok.Setter;
import org.cloud.dimension.aop.SystemResourceAspect;
import org.cloud.dimension.filter.SecurityFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConfigurationProperties(prefix = "spring.security")
@Setter
@ComponentScan("org.cloud.dimension")
public class SecurityFilterConfig {

    private List<String> excludedAuthPages;
    private List<String> excludedCheckLoginBindApi;
    private Boolean ipLoginLockFlag;

    @Bean
    public FilterRegistrationBean<?> securityFilter() {
        FilterRegistrationBean<?> registration = new FilterRegistrationBean<>(
            new SecurityFilter(excludedAuthPages == null ? new ArrayList<>() : excludedAuthPages,
                excludedCheckLoginBindApi == null ? new ArrayList<>() : excludedCheckLoginBindApi, ipLoginLockFlag != null && ipLoginLockFlag));
        registration.addUrlPatterns("/*"); //
        registration.setName("securityFilter");
        registration.setOrder(0);
        return registration;
    }

    @Bean
    public SystemResourceAspect systemResourceAspect() {
        return new SystemResourceAspect(excludedAuthPages);
    }
}
